package org.sys.transporttrainsys.service;

import org.sys.transporttrainsys.entity.TrainingType;
import org.sys.transporttrainsys.repository.TrainingTypeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TrainingTypeService {

    @Autowired
    private TrainingTypeRepository trainingTypeRepository;

    public List<TrainingType> getAllTrainingTypes() {
        return trainingTypeRepository.findAll();
    }

    public TrainingType getTrainingTypeById(Long id) {
        return trainingTypeRepository.findById(id).orElse(null);
    }

    public TrainingType createTrainingType(TrainingType trainingType) {
        return trainingTypeRepository.save(trainingType);
    }

    public TrainingType updateTrainingType(Long id, TrainingType trainingTypeDetails) {
        TrainingType trainingType = trainingTypeRepository.findById(id).orElse(null);
        if (trainingType != null) {
            trainingType.setTypeName(trainingTypeDetails.getTypeName());
            return trainingTypeRepository.save(trainingType);
        }
        return null;
    }

    public void deleteTrainingType(Long id) {
        trainingTypeRepository.deleteById(id);
    }

    public List<TrainingType> searchByTypeName(String name) {
        return trainingTypeRepository.findByTypeNameContainingIgnoreCase(name);
    }
}